﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Songhay.Silverlight.Mvvm.Views
{
    /// <summary>
    /// Displays error messages for the application.
    /// </summary>
    public partial class ErrorChildWindow : ChildWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorChildWindow"/> class.
        /// </summary>
        /// <param name="e">The exception.</param>
        /// <param name="errorSubtitle">The error subtitle.</param>
        public ErrorChildWindow(Exception e, string errorSubtitle = null)
        {
            InitializeComponent();

            this.SetErrorSubtitle(errorSubtitle);
            this.SetEvents();

            if(e != null)
            {
                this.ErrorTextBox.Text =
                    string.Concat(e.Message,
                        Environment.NewLine,
                        Environment.NewLine,
                        e.StackTrace);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorChildWindow"/> class.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="errorSubtitle">The error subtitle.</param>
        public ErrorChildWindow(Uri uri, string errorSubtitle = null)
        {
            InitializeComponent();

            this.SetErrorSubtitle(errorSubtitle);
            this.SetEvents();

            if(uri != null)
            {
                ErrorTextBox.Text =
                    string.Format("Page not found: “{0}”",
                        uri.ToString());
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorChildWindow"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="details">The details.</param>
        /// <param name="errorSubtitle">The error subtitle.</param>
        public ErrorChildWindow(string message, string details, string errorSubtitle = null)
        {
            InitializeComponent();

            this.SetErrorSubtitle(errorSubtitle);
            this.SetEvents();

            ErrorTextBox.Text =
                string.Concat(message,
                    Environment.NewLine,
                    Environment.NewLine, details);
        }

        void SetErrorSubtitle(string errorSubtitle)
        {
            if(errorSubtitle == null) return;
            this.ErrorSubTitle.Text = errorSubtitle;
        }

        void SetEvents()
        {
            this.OKButton.Click += (s, args) =>
            {
                this.DialogResult = true;
            };
        }
    }
}
